<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Apages extends Controller_Acommon {

	public $template = 'alayout/amain';

    public function params() {
        return array(
            'waymark' => 'title',
            'fields' => array(
                'link' => array(
                    'title' => 'Ссылка',
                    'type' => 'text',
                    'name' => 'link',
                ),
                'title' => array(
                    'title' => 'Заголовок',
                    'type' => 'text',
                    'name' => 'title',
                ),
                'text' => array(
                    'title' => 'Текст',
                    'type' => 'tinymce',
                    'name' => 'text',
                )
            ),
        );
    }

	public function action_index() {
        $params = $this->params();
		$this->showDefaultList(); // Рендерим список данных и форму редактирования\добавления
        $this->showPagination(); // Пагинация
        $this->showDefaultForm($params, ORM::factory($this->request->controller()));
	}

    public function action_add() {
        $newObj = ORM::factory($this->request->controller());
        $newObj->link = $this->request->post('link');
        $newObj->title = $this->request->post('title');
        $newObj->text = $this->request->post('text');
        $newObj->save();
        $this->request->redirect('administration/'.$this->request->controller().'/'.$this->request->param('page'));
    }

    public function action_edit() {
        $obj = ORM::factory($this->request->controller(), $this->request->param('id'));

        if ($this->request->param('save')) {
            $obj->link = $this->request->post('link');
            $obj->title = $this->request->post('title');
            $obj->text = $this->request->post('text');
            $obj->save();
            $this->request->redirect('administration/'.$this->request->controller().'/'.$this->request->param('page').'/edit/'.$this->request->param('id'));
            exit();
        }

        $params = $this->params($obj);
        $this->showDefaultList(); // Рендерим список данных и форму редактирования\добавления
        $this->showPagination(); // Пагинация
        $this->showDefaultForm($params, $obj, 'edit');
    }

    public function action_delete() {
        $this->obj = ORM::factory($this->request->controller(), $this->request->param('id'));
        $this->obj->delete();
        $this->request->redirect('administration/'.$this->request->controller().'/'.$this->request->param('page').'/edit/'.$this->request->param('id'));
    }
}